<?php
    error_reporting(E_ALL & ~E_DEPRECATED); 
    require_once("condb.php");     
    require_once("functions/functions_ring.php");     
    require_once('libs/nusoap.php');
    
    $server = new soap_server();
    
    $ns = "urn:RingServices";
    $server->configureWSDL("RingServices", $ns);
    $server->wsdl->schemaTargetNamespace = $ns;    
    
    $server->wsdl->addComplexType(
        'response'
        , 'complexType'
        , 'struct'
        , 'all'
        , ''
        , array(
            'status'=>array(
                'name'=>'status'
                , 'type'=>'xsd:int'
            ), 
            'code'=>array(
                'name'=>'code'
                , 'type'=>'xsd:string'
            ),
            'mobile'=>array(
                'name'=>'mobile'
                , 'type'=>'xsd:string'
            )            
        )
    );           
    
    $server->register(
        "call"
        , array("content"=>"xsd:string", "sender"=>"xsd:string", "serviceCode"=>"xsd:string", "transactionId"=>"xsd:string")
        , array("return"=> "tns:response")
//        , array("return"=> "xsd:string")
        , $ns
        , $ns . "#call"
        , "rpc"            
        , "encoded"
        , "Examination Results"  
    ); 
     
    function call($content, $sender, $serviceCode, $transactionId)
    {
        $ip = getRealIpAddr();
        $IpPublic = IpPublic();
        $response = array();
        if(in_array($ip, $IpPublic)){
            
            $data = array();
            $data["content"] = preg_replace("/(\s+)/si", " ", $content);        
            $data["sender"] = $sender;
            $data["serviceCode"] = $serviceCode;
            $data["transactionId"] = $transactionId;    
            
            /* Kiểm tra transaction */
            $checkTransaction = CheckTransaction($transactionId);
            
            if($checkTransaction==0){
                
                $cp = false;
                
                /*
                * check các cú pháp của app
                */
                
                $syntax = getSmsSyntax();
                $list_syntax = "";
                foreach($syntax as $row){
                    $list_syntax .= "|" . $row["syntax"];
                }
                $list_syntax = ltrim($list_syntax, "|");
                   
                if(!empty($list_syntax) && preg_match("/^(" . $list_syntax . ")/si", $data["content"])) {
                    /*
                    * Lấy type xem là loại app gì
                    */
                    preg_match("/^(" . $list_syntax . ")/si", $data["content"], $match);    
                    $syntax_code_db = !empty($match[1]) ? $match[1] : "";
                    $st = getTypeSyntax($syntax_code_db);
                    
                    if(!empty($st)){                        
                        $data["type_syntax"] = $st["type"];
                        $data["syntax_code_db"] = $syntax_code_db;
                        $data["service_code_db"] = $st["serviceCode"];
                        
                        if($st["type"]=="4" || $st["type"]=="5"){
                            /*
                            * nhạc chờ HDC
                            */
                            $cp = true;
                            require_once("functions/functions_ring.php");   
                        }
                    }
                } 
                
                if($cp == true){
                    $response = processing($data);    
                } else {
                    $response["status"] = 0;            
                    $response["code"] = "";    
                }
                
                
            } else {
                $response["status"] = 0;            
                $response["code"] = "";    
            }        
        } else {
            $response["status"] = 0;            
            $response["code"] = "";                
        }       
            
        return $response;       
    }       

    $HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : ""; 
    $server->service($HTTP_RAW_POST_DATA);
    exit();
?>